package com.zretc.internalreferral.controller;

import com.zretc.api.pojo.InternalReferral;
import com.zretc.common.constant.SecurityConstant;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.internalreferral.service.InternalReferralService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ir")
public class InternalReferralController {
    @Autowired
    private InternalReferralService internalReferralService;
    @GetMapping("/queryInternalReferralTitleAndDateByAmount")
    public RespResult queryInternalReferralTitleAndDateByAmount(@RequestParam("index") Integer index,
                                                                @RequestParam("pageNum")Integer pageNum)
    {
        List<InternalReferral> list=internalReferralService.queryIRsByAmount(index, pageNum);
        if(Collections.isEmpty(list))return R.fail(500,"服务器错误");
        List<Map<String, Object>> result = list.stream()
                .map(obj -> {
                    Map<String, Object> map = new HashMap<>();
                    // 假设MyObject类有getName和getAge方法
                    map.put("id", obj.getId());
                    map.put("title", obj.getTitle());
                    map.put("createdBy", obj.getCreatedBy());
                    map.put("updateAt", obj.getUpdateAt());
                    map.put("startTime", obj.getStartTime());
                    map.put("endTime", obj.getEndTime());
                    map.put("likeNum", obj.getLikeNum());
                    map.put("status", obj.getStatus());
                    map.put("isTopped", obj.isTopped());
                    // 为其他属性添加键值对
                    return map;
                })
                .collect(Collectors.toList());
        return R.success(result);
    }
    @GetMapping("/userQueryHisInternalReferralTitleAndDateByAmount")
    public RespResult userQueryHisInternalReferralTitleAndDateByAmount(@RequestParam("index")Integer index,
                                                                       @RequestParam("pageNum")Integer pageNum,
                                                                       HttpServletRequest req)
    {
        Integer createdBy= Integer.valueOf(req.getHeader(SecurityConstant.AUTHENTICATION_KEY));
        List<InternalReferral> list=internalReferralService.queryIRsByAmountByCreatorId(index, pageNum,createdBy);
        if(Collections.isEmpty(list))return R.fail(500,"服务器错误");
        List<Map<String, Object>> result = list.stream()
                .map(obj -> {
                    Map<String, Object> map = new HashMap<>();
                    // 假设MyObject类有getName和getAge方法
                    map.put("id", obj.getId());
                    map.put("title", obj.getTitle());
                    map.put("createdBy", obj.getCreatedBy());
                    map.put("updateAt", obj.getUpdateAt());
                    map.put("startTime", obj.getStartTime());
                    map.put("endTime", obj.getEndTime());
                    map.put("likeNum", obj.getLikeNum());
                    map.put("status", obj.getStatus());
                    map.put("passed", obj.getPassed());
                    // 为其他属性添加键值对
                    return map;
                })
                .collect(Collectors.toList());
        return R.success(result);
    }
    @GetMapping("/queryInternalReferralInfo")
    public RespResult queryInternalReferralInfo(@RequestParam Integer id)
    {
        InternalReferral temp=internalReferralService.queryIRById(id);
        if(Objects.isNull(temp))return R.fail(500,"服务器错误");
        Map<String,Object>map=new HashMap<>();
        map.put("id",temp.getId());
        map.put("title",temp.getTitle());
        map.put("content",temp.getContent());
        map.put("createdBy",temp.getCreatedBy());
        map.put("updatedAt",temp.getUpdateAt());
        map.put("startTime", temp.getStartTime());
        map.put("endTime", temp.getEndTime());
        map.put("likeNum", temp.getLikeNum());
        map.put("status", temp.getStatus());
        return R.success(map);
    }
    @PostMapping("/publishInternalReferral")
    public RespResult publishInternalReferral(@RequestBody InternalReferral internalReferral)
    {
        if(!internalReferralService.publishIR(internalReferral))R.fail(500,"服务器异常");
        return R.success(null);
    }
    @PostMapping("/userDeleteInternalReferral")
    public RespResult userDeleteInternalReferral(@RequestBody Map<String,Object> map,HttpServletRequest req)
    {
        Integer userId= Integer.valueOf(req.getHeader(SecurityConstant.AUTHENTICATION_KEY));
        Integer id=(Integer) map.get("id");
        if(!internalReferralService.deleteIROfUser(id,userId))R.fail(500,"服务器异常");
        return R.success(null);
    }
    @PostMapping("/adminApproInternalReferral")
    public RespResult adminApproInternalReferral(@RequestBody Map<String,Object> map)
    {
        Integer id=(Integer) map.get("id");
        Integer passed=(Integer)map.get("passed");
        Integer toUserId=(Integer)map.get("toUserId");
        String content=(String)map.get("content");
        if(!internalReferralService.approvalIR(id,passed,toUserId,content))R.fail(500,"服务器异常");
        return R.success(null);

    }
    @PostMapping("/topInternalReferral")
    public RespResult topInternalReferral(@RequestBody Map<String,Object> map)
    {
        Integer id= (Integer) map.get("id");
        boolean isTopped= (boolean) map.get("isTopped");
        if(!internalReferralService.topIR(id,isTopped))return R.fail(500,"置顶失败");
        return R.success(null);
    }
    @PostMapping("/userUpdateInternalReferral")
    public RespResult userUpdateInternalReferral(@RequestBody InternalReferral internalReferral,
                                                 HttpServletRequest req)
    {
        Integer updateBy= Integer.valueOf(req.getHeader(SecurityConstant.AUTHENTICATION_KEY));
        internalReferral.setUpdateBy(updateBy);
        if(!internalReferralService.updateIROfUser(internalReferral,updateBy))return R.fail(500,"更新失败");
        return R.success(null);

    }


}
